package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemContactsBusinessBinding;
import com.example.yichuangyun.databinding.LayoutEmptyList2Binding;
import com.example.yichuangyun.databinding.LayoutEmptyListBinding;
import com.example.yichuangyun.model.contacts.ContactsBusinessDataModel;
import com.example.yichuangyun.model.business.ProductModel;
import com.example.yichuangyun.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class ContactsBusinessAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private List<ContactsBusinessDataModel.ContactsBusinessModel> mData;

    public ContactsBusinessAdapter(Context context, List<ContactsBusinessDataModel.ContactsBusinessModel> data) {
        this.context = context;
        this.mData = data;
    }

    public void onRefresh(List<ContactsBusinessDataModel.ContactsBusinessModel> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            LayoutEmptyList2Binding emptyListBinding = LayoutEmptyList2Binding.inflate(LayoutInflater.from(context), parent, false);
            return new EmptyViewHolder(emptyListBinding);
        } else {
            ItemContactsBusinessBinding itemContactsBusinessBinding = ItemContactsBusinessBinding.inflate(LayoutInflater.from(context), parent, false);
            return new MyViewHolder(itemContactsBusinessBinding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            ContactsBusinessDataModel.ContactsBusinessModel model = mData.get(position);
            myViewHolder.binding.line.setVisibility(position == mData.size() - 1 ? View.INVISIBLE : View.VISIBLE);
            myViewHolder.binding.tvBusinessId.setText(String.format(context.getString(R.string.contact_business_id), model.getId()));
            myViewHolder.binding.tvCompanyName.setText(CommonUtil.checkText(model.getCompName()));
            myViewHolder.binding.tvStatus.setText(CommonUtil.checkText(model.getStepStatusName()));
            myViewHolder.binding.tvContactName.setText(String.format(context.getString(R.string.contact_name_mao), model.getUsername()));
            myViewHolder.binding.tvContactWay.setText(String.format(context.getString(R.string.contact_way_mao), model.getTelNumber()));
            myViewHolder.binding.tvCreateName.setText(String.format(context.getString(R.string.create_name_mao), model.getFoundUser()));
            myViewHolder.binding.tvCreateTime.setText(String.format(context.getString(R.string.create_date_mao), model.getCreateTime().substring(0, 10)));
            myViewHolder.binding.tvFollowName.setText(String.format(context.getString(R.string.follow_name_mao), model.getGdUser()));
            myViewHolder.binding.tvFollowTime.setText(String.format(context.getString(R.string.last_follow_date_mao), CommonUtil.setText(model.getLastFollowTime())));
            if (model.getProductList() != null && model.getProductList().size() > 0) {
                List<ProductModel> pList = model.getProductList();
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < pList.size(); i++) {
                    ProductModel productModel = pList.get(i);
                    String pName = TextUtils.isEmpty(productModel.getProductFullName()) ? productModel.getProductName() : productModel.getProductFullName();
                    builder.append(pName).append(i != pList.size() - 1 ? "\n" : "");
                }
                myViewHolder.binding.tvProductName.setText(builder.toString());
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mData.size() == 0) {
            return 1;
        }
        //如果不为0，按正常的流程跑
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        //在这里进行判断，如果我们的集合的长度为0时，我们就使用emptyView的布局
        if (mData.size() == 0) {
            return 0;
        }
        //如果有数据，则使用ITEM的布局
        return 1;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemContactsBusinessBinding binding;

        public MyViewHolder(ItemContactsBusinessBinding itemContactsBusinessBinding) {
            super(itemContactsBusinessBinding.getRoot());
            binding = itemContactsBusinessBinding;
        }
    }

    static class EmptyViewHolder extends RecyclerView.ViewHolder {

        LayoutEmptyList2Binding binding;

        public EmptyViewHolder(LayoutEmptyList2Binding emptyListBinding) {
            super(emptyListBinding.getRoot());
            binding = emptyListBinding;
        }
    }
}
